package indi.yuluo.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import indi.yuluo.core.RedisTemplate;
import indi.yuluo.dao.UserMapper;
import indi.yuluo.entity.user.UserInfo;
import indi.yuluo.service.UserRoleService;
import indi.yuluo.service.UserService;
import indi.yuluo.util.MD5Util;
import indi.yuluo.util.SnowFlakes;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.SimpleTimeZone;
import java.util.Timer;
import java.util.concurrent.TimeUnit;

/**
 * @Author: yuluo
 * @CreateTime: 2022-07-19  19:54
 * @Description: TODO
 */

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Resource
    private UserRoleService userRoleService;

    private static final RedisTemplate redisTemplate = new RedisTemplate();

    /**
     * 根据用户id查询用户信息
     * @param username 用户名
     * @return 用户对象
     */
    @Override
    public UserInfo getUserByUsername(String username) {

        return userMapper.getUserByUsername(username);
    }

    /**
     * 保存用户信息
     * @param username
     * @param password
     * @return
     */
    @Override
    public void saveUser(String username, String password) {

        // 利用雪花算法生成用户id
        Long UserId = SnowFlakes.getSnowFlakeUUId();

        // 加密密码
        password = MD5Util.getMD5(password);

        UserInfo userInfo = new UserInfo();
        userInfo.setUuid(UserId);
        userInfo.setUsername(username);
        userInfo.setPassword(password);

        userMapper.saveUser(userInfo);


        // 设置用户的默认权限
        userRoleService.saveUserRole(UserId);
    }

    /**
     * 用户自己添加自定义信息
     * @param userInfo
     * @return
     */
    @Override
    public boolean addUserInformation(UserInfo userInfo) {

        boolean flag = false;

        log.info("业务层的userinfo: {}", userInfo);

        // 组装用户更新时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String time = simpleDateFormat.format(System.currentTimeMillis());
        try {
            userInfo.setGmtModified(simpleDateFormat.parse(time));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        int i = userMapper.updateUserInfo(userInfo);
        if (i != 0) {
            flag = true;
        }

        return flag;
    }

    /**
     * 根据redis中的token从mysql中查出来用户信息
     * @param token
     * @return
     */
    @Override
    public UserInfo getUserByToken(String token) {

        UserInfo userByToken = redisTemplate.getObject(token, new TypeReference<UserInfo>() {});

        String username = userByToken.getUsername();

        return userMapper.getUserByUsername(username);

    }

    /**
     * 保存用户头像地址
     * @param userInfo
     */
    @Override
    public void saveUserAvatarAddress(UserInfo userInfo) {

        userMapper.saveUserAvatarAddress(userInfo.getUsername(), userInfo.getAvatar());
    }

    @Override
    public UserInfo getUserById(Long uid) {
        return userMapper.getUserById(uid);
    }


}
